package com.azzcs.consumer.controller;

import com.azzcs.provider.service.IProviderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @Author: wzg
 * @Date: 2021/2/6 上午10:09
 */
@Slf4j
@RestController
public class ConsumerController {
    @DubboReference
    private IProviderService providerService;

    private ExecutorService executorService = Executors.newCachedThreadPool();

    @GetMapping("/test")
    public String test(){
        return providerService.methodA();
    }

    @GetMapping
    public void execute() throws InterruptedException {
        int i = 0;
        long currentTime = System.currentTimeMillis();
        long endTime = currentTime + 1L * 60 * 1000;
//        long endTime = currentTime + 60L * 1000;
        long waitTime = 20;
        log.info("执行间隔"+waitTime+"毫秒");
        while (System.currentTimeMillis() - endTime < 0) {

            TimeUnit.MILLISECONDS.sleep(waitTime);

            executorService.execute(() -> providerService.methodA());
            executorService.execute(() -> providerService.methodB());
            executorService.execute(() -> providerService.methodC());

            i++;
        }
        log.info("总执行次数"+i);
    }
}
